<template>
  <!--弹窗-->
  <el-dialog :title="title" :visible.sync="Visible" :before-close="handleClose" width="600px">
    <!-- status-icon 当表单校验不通过时, 输入框右侧有个 x 小图标 -->
    <el-form
      status-icon
      ref="formData"
      :rules="rules"
      :model="formData"
      label-width="100px"
      label-position="right"
      style="width: 500px;"
    >
      <el-form-item label="广告图片:" prop="imageUrl">
        <el-upload
          class="avatar-uploader"
          action
          :show-file-list="false"
          :http-request="uploadMainImg"
        >
          <img v-if="formData.imageUrl" :src="formData.imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="广告标题:" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="广告链接:" prop="advertUrl">
        <el-input v-model="formData.advertUrl" />
      </el-form-item>
      <el-form-item label="跳转方式:" prop="advertTarget">
        <el-select v-model="formData.advertTarget" clearable style="width: 185px">
          <el-option label="新窗口打开" value="_blank"></el-option>
          <el-option label="当前窗口打开" value="_self"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="广告位置:" prop="position">
        <el-input v-model="formData.position" />
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序:" prop="sort">
        <el-input-number style="width: 300px;" v-model="formData.sort" :min="1" :max="10000" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="handleClose">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitForm('formData')">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import common from "@/api/common.js";
import advert from "@/api/advert.js";

export default {
  props: {
    title: {
      type: String,
      default: ""
    },
    Visible: {
      type: Boolean,
      default: true
    },
    formData: {
      type: Object,
      default: {}
    },
    remoteClose: Function,
    oldImageUrl: String // 修改前的照片url  编辑时 查询出来的图片
    // 如果点击编辑 点击上传图片 会把上一个图片删除 保留现在的 但是没有点击提交 不能吧这个图片和下面的文字绑定在一块
    // 所以保留下最初的图 防止丢失
  },
  data() {
    return {
      rules: {
        imageUrl: [{ required: true, message: "请上传图片", trigger: "blur" }],
        title: [{ required: true, message: "请输入广告标题", trigger: "blur" }],
        advertUrl: [
          { required: true, message: "请输入广告链接", trigger: "blur" }
        ],
        advertTarget: [
          { required: true, message: "请选择跳转方式", trigger: "blur" }
        ],
        position: [
          { required: true, message: "请输入广告位置", trigger: "blur" }
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
        sort: [{ required: true, message: "请选择排序", trigger: "blur" }]
      }
    };
  },
  methods: {
    handleClose() {
      // 清空表单
      this.$refs["formData"].resetFields();
      this.remoteClose();
    },
    // 上传图片 file上传的图片对象
    uploadMainImg(file) {
      const data = new FormData();
      data.append("file", file.file);
      common
        .uploadImg(data)
        .then(res => {
          this.deleteImg();
          this.formData.imageUrl = res.data;
          console.log(res, "uploadimg");
        })
        .catch(error => {
          this.$message({
            type: "error",
            message: "异常"
          });
        });
      console.log(file, "file");
    },
    // 删除之前的图片
    deleteImg() {
      if (
        this.formData.imageUrl &&
        this.formData.imageUrl !== this.oldImageUrl
      ) {
        common.deleteImg(this.formData.imageUrl).then(res => {
          console.log("删除上一个图片成功");
        });
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 校验通过，提交表单数据
          this.submitData();
          //   this.$refs[formName].resetFields();
          //   this.remoteClose();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async submitData() {
      let response = null;
      // id有id就是编辑的  因为数据时父元素返回的  没有id就是添加
      if (this.formData.id) {
        response = await advert.update(this.formData);
      } else {
        response = await advert.add(this.formData);
      }
      if (response.code == 20000) {
        this.$message({
          message: "保存成功",
          type: "success"
        });
        this.handleClose();
      } else {
        this.$message({
          message: "保存失败",
          type: "error"
        });
      }
    }
  }
};
</script>
<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>